﻿namespace Bowlus.Common
{
    using System;
    using System.Collections.Generic;
    using System.Web.Script.Serialization;


    using Microsoft.WindowsAzure.StorageClient;

    public class TenantDeployment : TableServiceEntity
    {
        private const string NoCustomization = "none";
        private const string Undefined = "undefined";

        public TenantDeployment()
            : base("_bowlus", Guid.NewGuid().ToString())
        {
            this.TenantId = Undefined;
            this.HostName = Undefined;
            this.Application = Undefined;
            this.Customizations = Undefined;
            this.AppHostInstanceId = Undefined;
            this.AppHostInstanceIp = Undefined;
            this.Status = Undefined;
            this.DeploymentAction = Undefined;
        }

        public TenantDeployment(string jsonString)
            : base("_bowlus", Guid.NewGuid().ToString())
        {
            var values = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(jsonString);
            this.TenantId = values["TenantId"];
            this.HostName = values["HostName"];
            this.Application = values["Application"];
            this.Customizations = values["Customizations"];
            this.AppHostInstanceId = values["AppHostInstanceId"];
            this.AppHostInstanceIp = values["AppHostInstanceIp"];
            this.Status = values["Status"];
        }

        /// <summary>
        /// The tenant identifier
        /// i.e. 'test1'
        /// </summary>
        public string TenantId
        {
            get
            {
                return this.RowKey;
            }

            set
            {
                this.RowKey = value;
            }
        }

        /// <summary>
        /// Tenant Host Name HOST_HEADER for provisioning and routing
        /// i.e. 'test1.bowlus.net'
        /// </summary>
        public string HostName { get; set; }

        /// <summary>
        /// This will point to the application version in the application container
        /// i.e. 'MyApp.1.2.zip'
        /// </summary>
        public string Application { get; set; }

        /// <summary>
        /// This will point to the customization"s" for the application in the customization container
        /// i.e. 'MyApp.1.2.1.zip'
        /// </summary>
        public string Customizations { get; set; }

        /// <summary>
        /// Azure RoleInstance Name Identifier
        /// i.e. 'Bowlus.AppHost_IN_0'
        /// </summary>
        public string AppHostInstanceId { get; set; }

        /// <summary>
        /// Azure RoleInstance IP
        /// i.e. '100.115.36.27'
        /// </summary>
        public string AppHostInstanceIp { get; set; }

        /// <summary>
        /// Tenant Status
        /// Contains values from TenantDeploymentStatatus class
        /// </summary>
        public string Status { get; set; }

        public string DeploymentAction { get; set; }

        public bool IncludesCustomizations
        {
            get
            {
                return !string.IsNullOrEmpty(this.Customizations) &&
                    this.Customizations.IndexOf(NoCustomization, StringComparison.InvariantCultureIgnoreCase) < 0;
            }
        }

        public IEnumerable<string> GetCustomizations()
        {
            var customizationList = new List<string>();
            if (this.IncludesCustomizations)
            {
                var items = this.Customizations.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                customizationList.AddRange(items);
            }

            return customizationList;
        }

        public TenantDeployment Clone()
        {
            var td = new TenantDeployment();
            td.TenantId = this.TenantId;
            td.HostName = this.HostName;
            td.Application = this.Application;
            td.Customizations = this.Customizations;
            td.AppHostInstanceId = this.AppHostInstanceId;
            td.AppHostInstanceIp = this.AppHostInstanceIp;
            td.Status = this.Status;
            td.DeploymentAction = this.DeploymentAction;

            return td;
        }

        public void DeepCopy(TenantDeployment deployment)
        {
            this.TenantId = deployment.TenantId;
            this.HostName = deployment.HostName;
            this.Application = deployment.Application;
            this.Customizations = deployment.Customizations;
            this.AppHostInstanceId = deployment.AppHostInstanceId;
            this.AppHostInstanceIp = deployment.AppHostInstanceIp;
            this.Status = deployment.Status;
            this.DeploymentAction = deployment.DeploymentAction;
        }

        public string Serialize()
        {
            var values = new Dictionary<string, string>()
                {
                    { "TenantId", this.TenantId },
                    { "HostName", this.HostName },
                    { "Application", this.Application },
                    { "Customizations", this.Customizations },
                    { "AppHostInstanceId", this.AppHostInstanceId },
                    { "AppHostInstanceIp", this.AppHostInstanceIp },
                    { "Status", this.Status },
                    { "Actions", this.DeploymentAction }
                };
            return new JavaScriptSerializer().Serialize(values);
        }
    }
}
